package com.mall.controller;

import com.mall.domain.Dict;
import com.mall.domain.ItemCat;
import com.mall.domain.ItemGoods;
import com.mall.domain.Page;
import com.mall.service.impl.DictServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 热词web层
 * @author : liangY
 * @date : 2021-01-14 0:24
 */
@Controller
@RequestMapping("/dicts")

public class DictController {

    @Autowired
    private DictServiceImpl dictService;

    /**
     * 查询前五热词
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(){
        Map<String,Object> dict = new HashMap<>();
        List<Dict> dicts = dictService.findAll();
        dict.put("dicts",dicts);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取热词成功");
        metaMap.put("status",200);
        dict.put("meta",metaMap);
        return dict;
    }
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody Dict dict){

        Map<String,Object> dictMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();

        //放入dataMap集合
        dataMap.put("dicts",dict);
        //放入Map集合
        dictMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = dictService.update(dict);
        if(flag > 0){
            metaMap.put("msg","添加商品成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新商品失败");
            metaMap.put("status",0);
        }
        dictMap.put("meta",metaMap);
        return dictMap;
    }


}
